package insight.google.game.era.data;

import insight.google.game.era.core.GameCenterManager;

import java.util.HashMap;
import java.util.List;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class HistoryDataManager extends AppDataManager {

	public HistoryDataManager(){
		super.initialize();
	}
	
	public HistoryDataManager(TransactionHandler txHandler){
		this.txHandler = txHandler;
		super.initialize();
	}
	
//	@Override
//	public boolean addToDataStore(Object o) {
//		try {
//			this.dataStoreMgr.save(o,pManager);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return false;
//	}

	@Override
	public List<GameCenterManager> findAll() {
		// TODO Auto-generated method stub
		try {
			return dataStoreMgr.get(GameCenterManager.class,pManager,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public GameCenterManager find() {
		List<GameCenterManager> gameCenter=this.findAll();
		if(gameCenter != null && gameCenter.size()>0 ){
			return gameCenter.get(0);
		}
		return null;
	}

	
	
//	public List<UserHistory> findUserHistoryByUserName(String userName){
//		StringBuffer cond = new StringBuffer();
//		cond.append(" userName== '" + userName + "'");
//		String orderBy = " loginTime desc ";
//		
//		List<UserHistory> userHistoryList = null;
//		try {
//			userHistoryList = DataStoreManager.getInstance().getByConditionNOrder(UserHistory.class, cond.toString(),pManager,this.isDetachable,orderBy);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return userHistoryList;
//	}
	
	public List<UserHistory> findUserHistoryByUserId(Key userId){
		StringBuffer cond = new StringBuffer();
		cond.append(" userId== :refId");
		String orderBy = " loginTime desc ";
		
		List<UserHistory> userHistoryList = null;
		try {
			userHistoryList = DataStoreManager.getInstance().getByConditionNOrder(UserHistory.class, cond.toString(),pManager,orderBy,userId,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userHistoryList;
	}
	
	
	public UserHistory findCurrentUSerHistory(Key userId){
		List<UserHistory> userHistoryList =  this.findUserHistoryByUserId(userId);
		if(userHistoryList != null && userHistoryList.size()>0){
			return userHistoryList.get(0);
		}
		return null;
	} 
	
	public List<GameHistoryDetail> findUsersGameHistoryByUserHistoryId(Key userHistoryId){
		StringBuffer cond = new StringBuffer();
		cond.append(" userHistoryId ==:refKey");
		List<GameHistoryDetail> userGameHistoryList = null;
		try {
			userGameHistoryList =this.dataStoreMgr.getByConditionWithKeyParam(GameHistoryDetail.class, cond.toString(), this.pManager,userHistoryId,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userGameHistoryList;
	}
	
	public List<GameHistory> findAllHistoryByRoomId(Key roomId){
		StringBuffer cond = new StringBuffer();
		cond.append(" roomId== '" + KeyFactory.keyToString(roomId) + "'");
		List<GameHistory> gameHistoryList = null;
		try {
			gameHistoryList =this.dataStoreMgr.getByCondition(GameHistory.class, cond.toString(), this.pManager,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return gameHistoryList;
	}
	
	public GameHistory findGameHistoryById(Key gameHistoryId){
		GameHistory gameHistory=null;
		try {
			gameHistory = (GameHistory)DataStoreManager.getInstance().getById(GameHistory.class, gameHistoryId, pManager,this.isDetachable);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return gameHistory;
	}
	
	public UserHistory findUserHistoryById(Key userHistoryId){
		UserHistory userHistory=null;
		try {
			userHistory = (UserHistory)DataStoreManager.getInstance().getById(UserHistory.class, userHistoryId, pManager,this.isDetachable);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userHistory;
	}

}
